package com.coffee.takeoutserver.controller;

import com.coffee.takeoutserver.bean.Order;
import com.coffee.takeoutserver.bean.Seller;
import com.coffee.takeoutserver.service.SellerService;
import com.coffee.takeoutserver.util.CommentResponse;
import com.coffee.takeoutserver.util.ResponseInfo;
import com.coffee.takeoutserver.service.OrderService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    SellerService sellerService;

    @RequestMapping("getOrderByUserId")
    @ResponseBody
    public String getOrderByUserId(int userId) {
        Gson gson = new Gson();
        List<Order> orders = orderService.getOrdersByUserId(userId);
        ResponseInfo res = new ResponseInfo();
        res.setCode("0");
        res.setData(gson.toJson(orderListToOrderResponseList(orders)));
        return gson.toJson(res);
    }

    @RequestMapping("getOrderBySellerId")
    @ResponseBody
    public String getOrderBySellerId(int sellerId) {
        Gson gson = new Gson();
        List<Order> orderList = orderService.getOrdersBySellerId(sellerId);
        ResponseInfo res = new ResponseInfo();
        res.setCode("0");
        Map<String, String> map = new HashMap<>();
        map.put("orderList", gson.toJson(orderListToOrderResponseList(orderList)));
        res.setData(gson.toJson(map));
        return gson.toJson(res);
    }

    private List<OrderResponse> orderListToOrderResponseList(List<Order> orderList) {
        List<OrderResponse> orderResponseList = new ArrayList<>();
        Gson gson = new Gson();
        for (Order order : orderList) {
            OrderResponse orderResponse = new OrderResponse();
            orderResponse.setId(order.getId());
            orderResponse.setDetail(order.getDetail());
            orderResponse.setDistribution(order.getDistribution());
            orderResponse.setGoodsinfos(order.getGoodsinfos());
            orderResponse.setRider("");
            orderResponse.setSeller(gson.toJson(sellerService.getSellerById(order.getSellerid())));
            orderResponse.setType(order.getType());
            orderResponse.setUserid(order.getUserid());
            orderResponse.setTime(order.getTime());
            orderResponse.setTotalMoney(order.getTotalmoney());
            orderResponse.setComment(order.getComment());
            orderResponseList.add(orderResponse);
        }
        return orderResponseList;
    }

    @RequestMapping("pay")
    @ResponseBody
    public String pay(@RequestBody Order order) {
        orderService.pay(order);
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setData("success");
        responseInfo.setCode("0");
        return new Gson().toJson(responseInfo);
    }

    @RequestMapping("getCommentListBySellerId")
    @ResponseBody
    public String getCommentListBySellerId(int sellerId) {
        Gson gson = new Gson();
        List<Order> orders = orderService.getOrdersBySellerId(sellerId);
        List<CommentResponse> commentResponseList = new ArrayList<>();
        for (Order order : orders) {
            String comment = order.getComment();
            if (null != comment && !comment.equals("")) {
                CommentResponse commentResponse = gson.fromJson(comment, CommentResponse.class);
                commentResponseList.add((commentResponse));
            }
        }
        ResponseInfo res = new ResponseInfo();
        res.setCode("0");
        Map<String, String> map = new HashMap<>();
        map.put("commentResponseList", gson.toJson(commentResponseList));
        res.setData(gson.toJson(map));
        return gson.toJson(res);
    }

    @RequestMapping("insertComment")
    @ResponseBody
    public String insertComment(int orderId, @RequestBody CommentResponse comment) {
        Gson gson = new Gson();
        orderService.insertComment(orderId, gson.toJson(comment));
        ResponseInfo res = new ResponseInfo();
        res.setCode("0");
        res.setData("");
        return gson.toJson(res);
    }
}
